package com.example.treasure_back.service;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.treasure_back.config.AliOSSConfig;
import com.example.treasure_back.entity.BusinessException;
import com.example.treasure_back.mapper.UserMapper;
import com.example.treasure_back.model.UserModel;
import com.example.treasure_back.model.vo.ImageUploadResult;
import com.example.treasure_back.utils.AliOSSUtils;
import com.example.treasure_back.utils.EmailUtil;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import java.io.IOException;
import java.io.InputStream;

@Service
public class UserService {
    @Resource
    private UserMapper userMapper;
    @Autowired
    private EmailUtil emailUtil;
    @Autowired
    private AliOSSUtils aliOSSUtils;
    @Autowired
    private AliOSSConfig aliOSSConfig;

    public Boolean register(String email, String password, String username){
        UserModel userModel = new UserModel();
        userModel.setEmail(email);
        userModel.setPassword(password);
        userModel.setUsername(username);
        userModel.setAvatar("https://4myblog.oss-cn-beijing.aliyuncs.com/treasure/avatar/enter.jpg");
        userModel.setIsValid(1);
        userModel.setCoin(0);
        int insert = userMapper.insert(userModel);
        return insert != 0;
    }

    public String sendEmail(String email){
        String code = null;
        UserModel userModel = userMapper.selectById(email);
        if(userModel == null){
            return null;
        }
        else{
            try {
                code = emailUtil.sendEmail(email);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return code;
        }
    }

    public String sendRegEmail(String email) throws MessagingException, IOException {
        String code = null;
        UserModel userModel = userMapper.selectById(email);
        if (userModel != null){
            return "-1";
        }
        else{
            code = emailUtil.sendEmail(email);
            return code;
        }
    }

    public boolean resetPassword(String email,String password){
        UserModel userModel = userMapper.selectById(email);
        UpdateWrapper<UserModel> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("password",password);
        updateWrapper.eq("email",email);
        userMapper.update(userModel,updateWrapper);
        return true;
    }

    public ImageUploadResult uploadAvatar(String email, InputStream inputStream, String suffix){
        String fileName = RandomStringUtils.randomAlphanumeric(32).toUpperCase();
        String avatar = aliOSSConfig.AVATAR_PATH + fileName + suffix;
        aliOSSUtils.uploadAvatar(inputStream,avatar);
        UserModel userModel = userMapper.selectById(email);
        UpdateWrapper<UserModel> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("email", email);
        updateWrapper.set("avatar",aliOSSConfig.PATH+avatar);
        userMapper.update(userModel,updateWrapper);
        return new ImageUploadResult(fileName,suffix);
    }

    public void resetUsername(String email,String username){
        UserModel userModel = userMapper.selectById(email);
        if (userModel == null){
            throw new BusinessException("-1", "用户不存在");
        }
        UpdateWrapper<UserModel> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("email",email);
        updateWrapper.set("username", username);
        userMapper.update(userModel,updateWrapper);
    }

    public String getAvatar(String email){
        UserModel userModel = userMapper.selectById(email);
        return userModel.getAvatar();
    }

    public String getUsername(String email){
        UserModel userModel = userMapper.selectById(email);
        return userModel.getUsername();
    }

}
